package cn.web.controller;

import cn.constant.ApiConstant;
import cn.constant.Messages;
import cn.entity.Article;
import cn.entity.Comment;
import cn.entity.User;
import cn.repository.CommentRepository;
import cn.service.ArticleService;
import cn.service.CommentService;
import cn.service.UserService;
import cn.util.AuthUtils;
import cn.web.model.ApiResult;
import cn.web.model.VmComment;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author m on 2023/1/29
 * Description:
 */
@Api(tags = "评论业务")
@RestController
@RequestMapping(ApiConstant.ROUTE_COMMENT_ROOT)
public class CommentApiController {
    private final CommentService commentService;
    private final ArticleService articleService;
    private final UserService userService;
    private final CommentRepository commentRepository;

    @Autowired
    public CommentApiController(CommentService commentService, ArticleService articleService, UserService userService, CommentRepository commentRepository) {
        this.commentService = commentService;
        this.articleService = articleService;
        this.userService = userService;
        this.commentRepository = commentRepository;
    }

    //新建评论(@用户评论)
    @ApiOperation("新建(@用户)评论")
    @PostMapping(value = ApiConstant.ROUTE_COMMENT_CREATE,produces = ApiConstant.API_PRODUCES)
    public ApiResult createComment(@RequestBody JSONObject json){
        Comment comment = new Comment();
        Article article = articleService.getById(UUID.fromString(json.getString("article_id")));
        article.setCommentSize(commentService.countByArticle(article));
        comment.setArticle(article);
        comment.setUser(userService.getById(AuthUtils.getUser(json.getString(ApiConstant.KEY_TOKEN)).getId()));
        comment.setContent(json.getString(ApiConstant.KEY_COMMENT_CONTENT));
        comment.setCreateTime(new Date());
        VmComment vm = new VmComment();
        String token = json.getString(ApiConstant.KEY_COMMENT_REPLY_COMMENT_ID);
        if(token==null || "".equals(token)){
            return new ApiResult(true, Messages.SUCCESS.toString(),vm.create(commentService.createComment(comment),false));
        }else{
            Comment replyComment = commentService.getById(UUID.fromString(token));
            commentService.replyComment(comment,replyComment);
            return new ApiResult(true, Messages.SUCCESS.toString(),vm.create(commentService.createComment(comment),true));
        }

    }


    //删除评论
    @ApiOperation("删除评论")
    @DeleteMapping(value = ApiConstant.ROUTE_COMMENT_DELETE)
    public ApiResult deleteComment(@RequestBody JSONObject json){
        String token = json.getString(ApiConstant.KEY_TOKEN);
        if(StringUtils.isEmpty(token)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(),null);
        }
        try{
            UUID id = UUID.fromString(json.getString(ApiConstant.KEY_UUID));
            Messages msg = commentService.deleteComment(id,token);
            if(!Messages.SUCCESS.equals(msg)){
                return new ApiResult(false,msg.toString(),null);
            }else{
                return new ApiResult(true,msg.toString(),null);
            }
        } catch (Exception e) {
            return new ApiResult(false, Messages.WRONG_ID.toString(),e.getMessage());
        }
    }
    //所有
    @ApiOperation("获取所有评论(测试用)")
    @GetMapping(value = "/")
    public ApiResult get(){
        List<Comment> comments = commentRepository.findAll();

        return new ApiResult(true, Messages.SUCCESS.toString(),comments);
    }

    //find
    @ApiOperation("根据id查找(测试用)")
    @GetMapping(value = "/{id}")
    public ApiResult find(@PathVariable UUID id){
        Comment comment = commentRepository.findById(id).orElse(null);
        return new ApiResult(true, Messages.SUCCESS.toString(),comment.getReplyComment());
    }
}
